package zhihui.mobile.ui.shop.order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zgm.utils.DateUtils;
import com.zgm.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.order.ShopOrder;
import zhihui.mobile.config.Config;

/**
 * Created by 张公铭 on 2017/8/17 0017.
 */

public class ShopOrderManagerItemAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater;
    private List<ShopOrder> mList = new ArrayList<>();
    private IButtonOnClickListener listener;

    public interface IButtonOnClickListener
    {
        void goOrderDetail(String orderId);

        void cancleOrder(String orderId);

        void goPay(String orderId, int moeny);

        void returnOrder(String orderId);

        void confirmOrder(String orderId);

        void aginBuy(String orderId);
    }

    public void setListener(IButtonOnClickListener listener)
    {
        this.listener = listener;
    }

    public ShopOrderManagerItemAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setmList(List<ShopOrder> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return Long.parseLong(mList.get(position).getOrderId());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView tv_time, tv_status, tv_num, tv_price, tv_price_time, tv_name, tv_specification, tv_shop_price, tv_shop_num;
        ImageView iv_icon;
        Button btn_one, btn_two;
        LinearLayout ll_content, ll_one, ll_root;
        RelativeLayout rl_more;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.shop_order_manager_item, null);
            ll_root = (LinearLayout) convertView.findViewById(R.id.ll_order_manager_item_root);
            tv_time = (TextView) convertView.findViewById(R.id.tv_shop_order_manager_item_time);
            tv_status = (TextView) convertView.findViewById(R.id.tv_shop_order_manager__item_status);
            tv_num = (TextView) convertView.findViewById(R.id.tv_shop_order_manager_item_count);
            tv_price = (TextView) convertView.findViewById(R.id.tv_shop_order_manager_item_price);
            tv_price_time = (TextView) convertView.findViewById(R.id.tv_shop_order_manager_item_price_time);

            iv_icon = (ImageView) convertView.findViewById(R.id.iv_shop_order_manager_item_img);
            tv_name = (TextView) convertView.findViewById(R.id.tv_shop_order_manager_item_name);
            tv_specification = (TextView) convertView.findViewById(R.id.tv_shop_order_manager_item_specification);
            tv_shop_price = (TextView) convertView.findViewById(R.id.tv_shop_order_manager_item_shop_price);
            tv_shop_num = (TextView) convertView.findViewById(R.id.tv_shop_order_manager_item_shop_num);

            btn_one = (Button) convertView.findViewById(R.id.btn_shop_order_manager_item_one);
            btn_two = (Button) convertView.findViewById(R.id.btn_shop_order_manager_item_two);
            ll_content = (LinearLayout) convertView.findViewById(R.id.ll_shop_order_manager_item_shops);
            ll_one = (LinearLayout) convertView.findViewById(R.id.ll_shop_order_manager_content_one);
            rl_more = (RelativeLayout) convertView.findViewById(R.id.rl_shop_order_manager_item_more);

            ViewCache viewCache = new ViewCache();
            viewCache.ll_root = ll_root;
            viewCache.tv_time = tv_time;
            viewCache.tv_status = tv_status;
            viewCache.tv_num = tv_num;
            viewCache.tv_price = tv_price;
            viewCache.tv_price_time = tv_price_time;
            viewCache.iv_icon = iv_icon;
            viewCache.tv_name = tv_name;
            viewCache.tv_shop_price = tv_shop_price;
            viewCache.tv_shop_num = tv_shop_num;
            viewCache.tv_specification = tv_specification;
            viewCache.btn_one = btn_one;
            viewCache.btn_two = btn_two;
            viewCache.ll_content = ll_content;
            viewCache.ll_shop_one = ll_one;
            viewCache.rl_more = rl_more;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            ll_root = viewCache.ll_root;
            tv_time = viewCache.tv_time;
            tv_status = viewCache.tv_status;
            tv_num = viewCache.tv_num;
            tv_price = viewCache.tv_price;
            tv_price_time = viewCache.tv_price_time;
            iv_icon = viewCache.iv_icon;
            tv_name = viewCache.tv_name;
            tv_specification = viewCache.tv_specification;
            tv_shop_num = viewCache.tv_shop_num;
            tv_shop_price = viewCache.tv_shop_price;
            btn_one = viewCache.btn_one;
            btn_two = viewCache.btn_two;
            ll_content = viewCache.ll_content;
            ll_one = viewCache.ll_shop_one;
            rl_more = viewCache.rl_more;
        }

        final ShopOrder shopOrder = mList.get(position);

        ll_root.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                listener.goOrderDetail(shopOrder.getOrderId());
            }
        });
        tv_time.setText(DateUtils.formatTime(shopOrder.getTime()));
        tv_status.setText(ShopOrder.getStatus(shopOrder.getStatus()));
        tv_num.setText("共" + shopOrder.getNum() + "件");

        switch (shopOrder.getStatus())
        {
            case 1:
                btn_one.setText("去支付");
                tv_price.setText("需付：" + ((double) shopOrder.getPaymoney() / 100) + "");
                btn_one.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        listener.goPay(shopOrder.getOrderId(), shopOrder.getPaymoney());
                    }
                });

                btn_two.setText("取消订单");
                btn_two.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        listener.cancleOrder(shopOrder.getOrderId());
                    }
                });
                break;
            case 2:
            case 3:
                tv_price.setText("实付：" + ((double) shopOrder.getPaymoney() / 100) + "");
                btn_one.setText("确定收货");
                btn_one.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        listener.confirmOrder(shopOrder.getOrderId());
                    }
                });
                if ((System.currentTimeMillis() - shopOrder.getTime()) > 7 * 24 * 60 * 60 * 1000)
                {
                    btn_two.setVisibility(View.GONE);
                } else
                {
                    btn_two.setText("退货退款");
                    btn_two.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            listener.returnOrder(shopOrder.getOrderId());
                        }
                    });
                }
                break;
            case 4:
                tv_price.setText("实付：" + ((double) shopOrder.getPaymoney() / 100) + "");

                if ((System.currentTimeMillis() - shopOrder.getTime()) > 7 * 24 * 60 * 60 * 1000)
                {
                    btn_one.setVisibility(View.GONE);
                } else
                {
                    btn_one.setText("申请退货");
                    btn_one.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View v)
                        {
                            listener.returnOrder(shopOrder.getOrderId());
                        }
                    });
                }
                btn_two.setVisibility(View.GONE);
                break;
            case 5:
            case 6:
            case 7:
                tv_price.setText("实付：" + ((double) shopOrder.getPaymoney() / 100) + "");
                btn_one.setText("再次购买");
                btn_one.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        listener.aginBuy(shopOrder.getOrderId());
                    }
                });
                btn_two.setVisibility(View.GONE);
                btn_one.setVisibility(View.GONE);
                break;
        }

        if (shopOrder.getGoodsNum() > 1)
        {
            rl_more.setVisibility(View.VISIBLE);
            ll_one.setVisibility(View.GONE);
            String[] imageSrc = shopOrder.getThumbnails().split(",");
            ll_content.removeAllViews();
            for (String src : imageSrc)
            {
                ImageView imageView = new ImageView(mContext);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(DensityUtil.dip2px(mContext, 60), DensityUtil.dip2px(mContext, 60));
                layoutParams.setMargins(0, DensityUtil.dip2px(mContext, 5), 0, 0);
                imageView.setLayoutParams(layoutParams);
                ImageLoader.getInstance().displayImage(Config.FILE_URL + src, imageView);
                ll_content.addView(imageView);
            }
        } else
        {
            rl_more.setVisibility(View.GONE);
            ll_one.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(Config.FILE_URL + shopOrder.getThumbnails().replace(",", ""), iv_icon);
            tv_name.setText(shopOrder.getTitle());
            tv_specification.setText(shopOrder.getSpecif());
            tv_shop_price.setText("￥" + ((double) shopOrder.getPrice() / 100));
            tv_shop_num.setText("×" + shopOrder.getGoodsNum());
        }
        return convertView;
    }

    class ViewCache
    {
        public TextView tv_time, tv_status, tv_num, tv_price, tv_price_time, tv_name, tv_specification, tv_shop_price, tv_shop_num;
        public ImageView iv_icon;
        public Button btn_one, btn_two;
        public LinearLayout ll_content, ll_shop_one, ll_root;
        public RelativeLayout rl_more;
    }
}
